import 'package:f_enjoy_plus/components/Header.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'controller/MyRepairVM.dart';

class MyrepairPage extends StatelessWidget {
  final vm = Get.put(MyRepairVM());
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: Header(title: '我的报修'),
      body: Container(
        padding: EdgeInsets.all(16),
        child: Column(
          children: [
            Expanded(
              child: Obx(() {
                if (vm.houseList.isEmpty) {
                  return Center(child: CircularProgressIndicator());
                }
                return ListView.builder(
                  itemCount: vm.repairList.length,
                  itemBuilder: (context, index) {
                    return MyRepairItem(index);
                  },
                );
              }),
            ),

            ElevatedButton(
              onPressed: () {
                Navigator.pushNamed(context, '/repair');
              },
              child: Text('添加报修单${vm.repairList.length}'),
            ),
          ],
        ),
      ),
    );
  }
}

Widget MyRepairItem(int index) {
  final vm = MyRepairVM.to;
  final repair = vm.repairList[index];

  if (vm.houseList.isEmpty) {
    return SizedBox();
  }

  final house = vm.houseList.firstWhere(
    (element) =>
        element.id.toString().trim() ==
        (repair.houseId ?? '').toString().trim(),
    orElse: () => HouseItemRes(id: '', name: '未知房屋'),
  );
  return Container(
    padding: EdgeInsets.all(16),
    margin: EdgeInsets.only(bottom: 16),
    decoration: BoxDecoration(
      color: Colors.white,
      borderRadius: BorderRadius.circular(8),
    ),
    child: Column(
      children: [
        Row(children: [Text(house.name)]),
        Row(children: [Text(repair.houseId ?? "")]),
      ],
    ),
  );
}
